%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Anpassung einer Errorfunktion an eine teilweise Abdeckung eines Gausstrahls.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function GaussstrahlVermessung
figure(1);			clf;

M=GaussstrahlVermessungData;									%x-Position der Rasierklinge versus Reduktion der Leistung

pa =[5 1.8];													%Fit einer Errorfunktion
pa =fminsearch(@GaussstrahlVermessungFit,pa,[],M);
Y  =erf(pa(1)+pa(2)*M(:,1))/2+.5;
pa(1)
pa(2)

GaussstrahlPropagation;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Subroutine für Daten.

function M = GaussstrahlVermessungData

M=[	-2.0	1.1e-1
	-1.8	6.3e-2
	-1.6	2.0e-2
	-1.4	3.7e-2
	-1.3	2.0e-2
	-1.2	1.3e-1
	-1.0	3.6e-3
	-0.8	1.2e-1
	-0.6	1.0e-1
	-0.4	1.2e-1
	-0.2	3.3e-1
	0.0		5.1e-1
	0.2		7.7e-1
	0.4		1.02
	0.6		1.12
	0.8		0.94
	1.0		1.02
	1.2		1.05
	1.4		1.01
	1.6		1.03
	1.8		1.06
	2.0		1.08	];


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Subroutine für Fit.

function chi = GaussstrahlVermessungFit(pa,M)

X=M(:,1);
P=M(:,2);

Y=erf(pa(1)+pa(2)*M(:,1))/2+.5;
chi=sum((Y-M(:,2)).^2);

subplot(221),plot(M(:,1),M(:,2),'+r',M(:,1),Y,'g','LineWidth',1.4)
set(gca,'yLim',[-.2 1.2],'FontSize',16,'LineWidth',1.4);
xlabel('\fontsize{16}{\itx}   (mm)');
ylabel('\fontsize{16}{\itP/P}_0');
pause(.01)